define([
        'app',
        './API'
    ],
    function (app) {
        'use strict';

        app.factory('UserAPI', ['API', '$localForage', '$resource', '$rootScope',
            function (API, $localForage, $resource, $rootScope) {
                var _API = new API('user');
                var server = _API.server;

                _API.request = {
                    smsCode: function (phoneNumber) {
                        return $resource(server + '/request/smsCode').save(
                            phoneNumber
                        ).$promise;
                    },

                    passwordByEmail: function (email) {
                        return $resource(server + '/request/passwordByEmail').save(
                            email
                        ).$promise;
                    },

                    passwordBySmsCode: function (email) {
                        return $resource(server + '/request/passwordBySmsCode').save(
                            email
                        ).$promise;
                    },

                    verifyPhoneBySmsCode: function (phoneNumber) {
                        return $resource(server + '/request/verifySmsCode').save(
                            phoneNumber
                        ).$promise;
                    }
                };
                _API.reset = {
                    password: function (user) {
                        return $resource(server + '/reset/password').save(user).$promise
                    },
                    passwordBySmsCode: function (user) {
                        return $resource(server + '/reset/passwordBySmsCode').save(user).$promise
                    },
                    passwordByToken: function (user) {
                        return $resource(server + '/reset/passwordByToken').save(user).$promise
                    }
                };

                _API.card = angular.extend(new API('user/card'), {});


                var _collect = $resource(server + '/collect/product', {}, {'delete': {method: 'DELETE'}});
                return angular.extend(_API, {
                    /**
                     * 登录
                     * @param user
                     * @returns {*|Function|promise|n}
                     */
                    login: function (user) {
                        return $resource(server + '/login').save(
                            user
                        ).$promise;
                    },
                    logout: function () {
                        return $resource(server + '/logout').get().$promise;
                    },
                    /**
                     * 手机登录
                     * @param user
                     * @returns {*}
                     */
                    loginWithPhone: function (user) {
                        return $resource(server + '/loginWithPhone').get(
                            user
                        ).$promise;
                    },

                    loginWithPlatform: function (auth) {
                        return $resource(server + '/loginWithPlatform').save(auth).$promise;
                    },
                    linkWithPlatform: function (auth) {
                        return $resource(server + '/linkWithPlatform').save(auth).$promise;
                    },
                    unlinkWithPlatform: function (auth) {
                        return $resource(server + '/unlinkWithPlatform').save(auth).$promise;
                    },
                    loginWithToken: function (token) {
                        return $resource(server + '/loginWithSessionToken/:token').get({token: token}).$promise;
                    },

                    /**
                     * 用邮箱注册
                     * @param user
                     * @returns {*}
                     */
                    registerWithEmail: function (user) {
                        return $resource(server + '/registerWithEmail').save(
                            user
                        ).$promise;
                    },

                    /**
                     * 得到当前用户
                     * @returns {*}
                     */
                    getCurrentUser: function () {
                        //TODO: 如果localForage 不支持，或是没得到需要去尝试cookie里是否有
                        return $localForage.getItem('currentUser');
                    },
                    /**
                     * 设置当前用户
                     * @param user
                     * @returns {*}
                     */
                    setCurrentUser: function (user) {
                        //TODO: 如果localForage 不支持，尝试写入cookie里
                        if (!user)
                            return $localForage.removeItem('currentUser');
                        if (user.password)
                            delete user.password;
                        return $localForage.setItem('currentUser', user);
                    },
                    /**
                     * 通过 objectjd 得到当前用户信息
                     * @param objectId
                     */
                    profile: function (objectId) {
                        return $resource(server + '/:objectId/profile').get(
                            {
                                objectId: objectId
                            }
                        ).$promise;
                    },
                    verifyAccount: function (user) {
                        return $resource(server + '/verify/account').get(user).$promise;
                    },
                    verifySmsCode: function (user) {
                        return $resource(server + '/verify/smscode').get(user).$promise;
                    },
                    getMsg: function (error) {
                        return '错误';
                    },
                    //collection
                    collect: function (productId) {
                        return _collect.save({objectId: productId}).$promise;
                    },
                    getCollection: function (userId, nameId) {//用户id,要取的属性名
                        return $resource(server + '/relations').get({objectId: userId, name: nameId}).$promise;
                    },
                    unCollect: function (productId) {
                        return _collect.delete({objectId: productId}).$promise;
                    },
                    collectionList: function () {
                        return $resource(server + '/collectList').get().$promise;
                    },
                    currentRoles: function () {
                        try {
                            return $rootScope.currentUser.roles;
                        } catch (e) {
                            return [];
                        }
                    },
                    feedback: function (feedback) {
                        return $resource(server + '/feedback').save(feedback).$promise;
                    }
                });
            }]);
    });
